package com.tpshion.cloud.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tpshion.cloud.auth.domain.dto.AddPerMissionDto;
import com.tpshion.cloud.auth.domain.dto.QueryPermissionDto;
import com.tpshion.cloud.auth.domain.dto.UpdatePermissionDto;
import com.tpshion.cloud.auth.domain.entity.Permission;
import com.tpshion.cloud.auth.domain.entity.RolePermission;
import com.tpshion.cloud.auth.mapper.PermissionMapper;
import com.tpshion.cloud.auth.mapper.RolePermissionMapper;
import com.tpshion.cloud.auth.service.PermissionService;
import com.tpshion.cloud.common.support.Assert;
import com.tpshion.cloud.common.support.ErrorCode;
import com.tpshion.cloud.common.utils.IdUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    /**
     * 分页查询功能权限
     * @param queryPermissionDto
     * @return
     */
    @Override
    public PageInfo queryPermission(QueryPermissionDto queryPermissionDto) {
        PageHelper.startPage(queryPermissionDto.getPageNum(),queryPermissionDto.getPageSize());
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        if(!StringUtils.isEmpty(queryPermissionDto.getName())){
            queryWrapper.like("name", queryPermissionDto.getName());
        }
        if(!StringUtils.isEmpty(queryPermissionDto.getPath())){
            queryWrapper.like("path", queryPermissionDto.getPath());
        }
        if(!StringUtils.isEmpty(queryPermissionDto.getSysCode())){
            queryWrapper.like("sys_code", queryPermissionDto.getSysCode());
        }
        List<Permission> list = permissionMapper.selectList(queryWrapper);
        return new PageInfo(list);
    }

    /**
     * 添加权限
     * @param addPerMissionDto
     * @return
     */
    @Override
    @Transactional
    public Permission addPermission(AddPerMissionDto addPerMissionDto) {
        Permission permission = new Permission();
        permission.setId(IdUtil.getUUID());
        BeanUtils.copyProperties(addPerMissionDto, permission);
        permission.setSysCode(addPerMissionDto.getSysCode());
        permission.setCreateTime(LocalDateTime.now());
        permissionMapper.insert(permission);
        return permission;
    }

    /**
     * 更新权限
     * @param updatePermissionDto
     * @return
     */
    @Override
    @Transactional
    public Permission updatePermission(UpdatePermissionDto updatePermissionDto) {
        Permission permission = permissionMapper.selectById(updatePermissionDto.getId());
        Assert.notNull(permission, ErrorCode.PERMISSION_NOT_EXIST);

        BeanUtils.copyProperties(updatePermissionDto, permission);
        permission.setSysCode(updatePermissionDto.getSysCode());
        permission.setUpdateTime(LocalDateTime.now());
        permissionMapper.updateById(permission);
        return permission;
    }

    /**
     * 删除权限
     * @param ids
     */
    @Override
    @Transactional
    public void deletePermission(List<String> ids) {
        // 先删除权限，再删除权限角色关系
        permissionMapper.deleteBatchIds(ids);
        ids.stream().forEach(permissionId -> {
            QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("permission_id", permissionId);
            rolePermissionMapper.delete(queryWrapper);
        });
    }
}
